package com.xjtu.platform.user.admin.time_set;

import com.xjtu.platform.common.*;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

import java.util.Map;

import static com.xjtu.platform.common.Result.ERR_CODE_BUSINESS;
import static com.xjtu.platform.common.Result.WARN_CODE_GO;

@RestController
@RequestMapping("/security/timeSet")
public class TimeSetAPI {
    @Resource
    TimeSetService timeSetService;

    @Resource
    private CommonService commonService;

    @RequestMapping(value = "/insertTime",method = RequestMethod.POST)
     Result timeAdd(@RequestBody TimeSetDto dto,@RequestHeader("Token") String Token){
        if(Token == null){
            System.out.println("查询当前用户时无令牌");
        }
        try {
            CurrentUser currentUser = TokenUtils.getUserInfo(Token, commonService);

        }catch (Exception e){
            e.printStackTrace();
        }

        if(!(timeSetService.checkTime(dto))){return Result.fail(ERR_CODE_BUSINESS,"时间冲突");}
         if(timeSetService.timeAdd(dto)){
             return Result.success("添加成功");
         }
         else return Result.fail(WARN_CODE_GO,"超出范围，是否要删除所有时间重新添加");
     }


    @RequestMapping(value = "/getTimeList",method = RequestMethod.POST)
    Result getTimeList(@RequestBody TimeSetDto dto, @RequestHeader("Token") String Token){

         if(Token == null){
             System.out.println("查询当前用户时无令牌");
         }
         try {
             CurrentUser currentUser = TokenUtils.getUserInfo(Token, commonService);


         }catch (Exception e){
             e.printStackTrace();
         }
        Map<String,Object> page = timeSetService.getInfo(dto);
        return Result.success(page);
     }
    @RequestMapping(value = "/changeTime",method = RequestMethod.POST)
    Result changeTime(@RequestBody TimeSetDto dto,@RequestHeader("Token") String Token){

        if(Token == null){
            System.out.println("查询当前用户时无令牌");
        }
        try {
            CurrentUser currentUser = TokenUtils.getUserInfo(Token, commonService);


        }catch (Exception e){
            e.printStackTrace();
        }

        if(!timeSetService.checkTime(dto)){return Result.fail(ERR_CODE_BUSINESS,"时间冲突");}
        timeSetService.changeTime(dto);
        return Result.success();
    }


    @RequestMapping(value = "/clearTime",method = RequestMethod.POST)
    Result clearTime(@RequestHeader("Token") String Token){

        if(Token == null){
            System.out.println("查询当前用户时无令牌");

        }
        try {
            CurrentUser currentUser = TokenUtils.getUserInfo(Token, commonService);


        }catch (Exception e){
            e.printStackTrace();
        }
        timeSetService.clearTime();
        return Result.success();
    }



}
